﻿using System.Collections.Specialized;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.Security;
using AgileNYCWebService.Manager.Data.Table;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PushSharp;
using PushSharp.Android;
using PushSharp.Core;
using PushSharp.Apple;

namespace AgileNYCWebService
{
	public partial class Default : System.Web.UI.Page
	{
		private PushBroker _push;
		protected void Page_Load(object sender, EventArgs e)
		{

			_push = new PushBroker();

			//Wire up the events for all the services that the broker registers
			_push.OnNotificationSent += NotificationSent;
			_push.OnChannelException += ChannelException;
			_push.OnServiceException += ServiceException;
			_push.OnNotificationFailed += NotificationFailed;
			_push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
			_push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
			_push.OnChannelCreated += ChannelCreated;
			_push.OnChannelDestroyed += ChannelDestroyed;
			//SendNotification();
		}

		private void SendNotification()
		{
			try
			{
				var appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data/AgileNYC-APN-Server.p12"));
				//IMPORTANT: If you are using a Development provisioning Profile, you must use the Sandbox push notification server 
				//  (so you would leave the first arg in the ctor of ApplePushChannelSettings as 'false')
				//  If you are using an AdHoc or AppStore provisioning profile, you must use the Production push notification server
				//  (so you would change the first arg in the ctor of ApplePushChannelSettings to 'true')
				_push.RegisterAppleService(new ApplePushChannelSettings(appleCert, "123")); //Extension method
				//Fluent construction of an iOS notification
				//IMPORTANT: For iOS you MUST MUST MUST use your own DeviceToken here that gets generated within your iOS app itself when the Application Delegate
				//  for registered for remote notifications is called, and the device token is passed back to you
				_push.QueueNotification(new AppleNotification()
										   .ForDeviceToken("73117055537c0664d931189b2d77f6953c59e22f5c6981ce5bc72b95943bf75c")
										   .WithAlert("Hello World!")
										   .WithBadge(7)
										   .WithSound("sound.caf"));
				_push.StopAllServices();
			}
			catch (Exception ex)
			{

			}

		}

		static void DeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, INotification notification)
		{
			//Currently this event will only ever happen for Android GCM
			Logger.Log.Info("Device Registration Changed:  Old-> " + oldSubscriptionId + "  New-> " + newSubscriptionId + " -> " + notification);
		}

		static void NotificationSent(object sender, INotification notification)
		{
			Logger.Log.Info("Sent: " + sender + " -> " + notification);
		}

		static void NotificationFailed(object sender, INotification notification, Exception notificationFailureException)
		{
			Logger.Log.Error("Failure: " + sender + " -> " + notificationFailureException.Message + " -> " + notification);
		}

		static void ChannelException(object sender, IPushChannel channel, Exception exception)
		{
			Logger.Log.Error("Channel Exception: " + sender + " -> " + exception);
		}

		static void ServiceException(object sender, Exception exception)
		{
			Logger.Log.Error("Channel Exception: " + sender + " -> " + exception);
		}

		static void DeviceSubscriptionExpired(object sender, string expiredDeviceSubscriptionId, DateTime timestamp, INotification notification)
		{
			Logger.Log.Error("Device Subscription Expired: " + sender + " -> " + expiredDeviceSubscriptionId);
		}

		static void ChannelDestroyed(object sender)
		{
			Logger.Log.Error("Channel Destroyed for: " + sender);
		}

		static void ChannelCreated(object sender, IPushChannel pushChannel)
		{
			Logger.Log.Info("Channel Created for: " + sender);
		}
	}

	public class Login
	{
		public string DeviceId { get; set; }
		public string Email { get; set; }
		public int SocialType { get; set; }
		public bool IsCheck { get; set; }
		public int MobileWorkingSystem { get; set; }
		public string Password { get; set; }

		public int DeviceOs { get; set; }
	}
}